package com.itheima.controller;

import com.itheima.annotation.Log;
import com.itheima.po.InsertEmp;
import com.itheima.po.LoginParam;
import com.itheima.po.SelectEmpPageParam;
import com.itheima.po.UpdateEmp;
import com.itheima.pojo.Result;
import com.itheima.service.EmpService;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/emps")
@CrossOrigin
public class EmpController {
    @Autowired
    EmpService empService;

    /**
     * 查询员工界面
     *
     * @param selectEmpPageParam
     * @return
     */
    @GetMapping
    public Result selectEmpPage(SelectEmpPageParam selectEmpPageParam) {
        return Result.success(empService.selectEmpPage(selectEmpPageParam));
    }

    /**
     * 批量删除接口
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    public Result deleteByIds(@RequestParam("ids") List<Integer> ids) {
        if (ids.isEmpty()){
            return Result.fail("请选择要删除的数据");
        }
        log.info("ids:{}", ids);
        return empService.deleteByIds(ids);
    }

    /**
     * 添加员工接口
     */
    @Log
    @PostMapping
    public Result addEmp(@Valid @RequestBody InsertEmp emp) {
        log.info("emp:{}", emp);
        int i = empService.addEmp(emp);
        return i > 0 ? Result.success("添加成功") : Result.fail("添加失败");
    }

    /**
     * 根据id查询员工数据
     */
    @GetMapping("/{id}")
    public Result selectById(@PathVariable Integer id) {
        log.info("id:{}", id);
        return Result.success(empService.selectById(id));
    }

    /**
     * 更新员工数据
     */
    @PutMapping
    @Log
    public Result updateEmp(@Validated @RequestBody UpdateEmp emp) {
        log.info("emp:{}", emp);
        int i = empService.updateEmp(emp);
        return i > 0 ? Result.success("更新成功") : Result.fail("更新失败");
    }

    /**
     * 查询全部员工
     */
    @GetMapping("/list")
    public Result selectAll() {
        return Result.success(empService.selectAll());
    }




}
